/**
 * Created by albert on 2017/11/7.
 */
/**
 * Created by albert on 2017/11/2.
 */
import Api from "../../api/api"
import * as types from "../mutation-types"
import  {setParams} from "../../util/methods"
const state = {
    //个人信息
    person: {
        basic: [
            {'key': '姓名', 'value': ''},
            {'key': '岗位', 'value': ''},
            {'key': '手机', 'value': ''},
            {'key': '身份证号', 'value': ''},
        ],
        created_at: ''
    },
    //还款计划
    repaymentplan: {},
    app_message: ''
}
const actions = {
    getPersonInfo({commit}, payload){
        return new Promise((resolve, reject) => {
            Api.personInfo('')
                .then(res => {
                    commit({
                        type: "personInfo",
                        res,
                    })
                    resolve(res)
                }, err => {
                    reject(err)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    getRepayment({commit}, payload){
        return new Promise((resolve, reject) => {
            Api.repayment({id_card: payload.id_card})
                .then(res => {
                    commit({
                        type: "repayment",
                        res,
                    })
                    resolve(res)
                }, err => {
                    reject(err)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    initrepayment({commit}, payload){
        return new Promise((resolve, reject) => {
            Api.repayment({id_card: payload.id_card})
                .then(res => {
                    commit({
                        type: 'initrepayment',
                        res,
                    })
                    resolve(res)
                }, err => {
                    reject(err)
                })
                .catch(err => {
                    reject(err)
                })
        })
    }
}

const mutations = {
    setMineParam(state, payload){
        state = setParams(state, payload.params)
    },
    resetParam(state){
        state = {
            //个人信息
            person: {
                basic: [
                    {'key': '姓名', 'value': ''},
                    {'key': '岗位', 'value': ''},
                    {'key': '手机', 'value': ''},
                    {'key': '身份证号', 'value': ''},
                ],
                created_at: ''
            },
            //还款计划
            repaymentplan: {}
        }
    },
    personInfo(state, payload){
        state.person = {
            basic: [
                {'key': '姓名', 'value': payload.res.username},
                {'key': '岗位', 'value': payload.res.job},
                {'key': '手机', 'value': payload.res.phone},
                {'key': '身份证号', 'value': payload.res.idcard},
            ],
            created_at: payload.res.created_at
        }

    },
    repayment(state, payload){
        state.repaymentplan = payload.res
    },
    initrepayment(state, payload){
        state.repaymentplan = payload.msg = ''
    },
    resetAppMessage(state, payload){
        state.app_message = ""
    },
    [types.APP_MESSAGE](state, payload){
        state.app_message = payload
    }
}
export default {
    state,
    actions,
    mutations,
}